//
//  main.m
//  HomeWork 05 排序、数组字典的处理
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    
//   一、
//     
//     1.创建1个包含5个字符串元素的数组；
//     2.用for循环倒序输出所有元素；
//     3.用for...in完成所有字符串元素的拼接(生成一个
//     新的字符串)，并输出拼接好的字符串。
    NSArray *arrOfStr = @[@"a",@"b",@"c",@"d",@"e"];
    for (NSInteger i = arrOfStr.count - 1; i >= 0; i--) {
        NSString *value = arrOfStr[i];
        NSLog(@"%@",value);
    }
    NSString *strOfplicing = [NSString string];
    for (NSString *str   in arrOfStr) {
        strOfplicing = [strOfplicing stringByAppendingString:str];
    }
    NSLog(@"拼接好的字符串:%@",strOfplicing);
    
//     二、
//     
//     1.创建1个包含5个person对象的数组(姓名用英文)；
//     2.用for循环找到年龄最大的Person对象；
//     3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [Person personWithName:@"zhangsan" age:50 gender:@"male"];
    Person *per2 = [Person personWithName:@"lisi" age:30 gender:@"male"];
    Person *per3 = [Person personWithName:@"wangwu" age:35 gender:@"male"];
    Person *per4 = [Person personWithName:@"tom" age:15 gender:@"female"];
    Person *per5 = [Person personWithName:@"zhaoyun" age:40 gender:@"female"];
    NSArray *arrOfPer = @[per1,per2,per3,per4,per5];
    NSInteger max = 0;
    int index = 0;
    for (int i = 0; i < 5; i++) {
        Person *per = [arrOfPer objectAtIndex:i];
        if (max < per.age ) {
            max = per.age;
            index = i;
        }
    }
    NSLog(@"最大的年龄:%ld",max);
    NSString *strOf = [NSString string];
    for (Person *per in arrOfPer) {
        if (per.age >= 40) {
       strOf = [per.name stringByAppendingString:@"-brother"];
            NSLog(@"大雨某个对象拼接结果:%@",strOf);
        }
    }
    
//     三、
//     1.使用第二题目中生成的数组；
//     2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//     3.使用NSSortDescriptor，按姓名进行排序。
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    arrOfPer = [arrOfPer sortedArrayUsingDescriptors:@[sort]];
    NSLog(@"3.按姓名排序:%@",arrOfPer);
    NSArray *arrByName = [arrOfPer sortedArrayUsingSelector:@selector(asSortedByName:)];
    NSLog(@"按姓名排序:%@",arrByName);
    NSArray *arrByGender = [arrOfPer sortedArrayUsingSelector:@selector(asSortedByGender:)];
    NSLog(@"按性别排序:%@",arrByGender);
    NSArray *arrByAge = [arrOfPer sortedArrayUsingSelector:@selector(asSortedByAge:)];
    NSLog(@"按年龄排序:%@",arrByAge);
//     四、
//      处理省市区文件中的area.txt
//     将省市区进行分割处理, 省里包含市, 市里包含区
//     1. 先用数组字典表示
//     2. 将数组字典的处理方式转换成数组模型(Class)
#if 1
    NSString *datastring = [NSString stringWithContentsOfFile:@"/Users/dllo/Downloads/Homework-05/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfPlace = [datastring componentsSeparatedByString:@"\n"];
    //创建一个省数组
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *name in arrOfPlace) {
        if (![name hasPrefix:@" "]) {
            //建立省字典
            NSMutableDictionary *dicOfPro = [NSMutableDictionary dictionary];
            [dicOfPro setObject:name forKey:@"proName"];
            //建立市数组
            NSMutableArray *cityArr = [NSMutableArray array];
            [dicOfPro setObject:cityArr forKey:@"cityArr"];
            [proArr addObject:dicOfPro];
            //NSLog(@"%@",dicOfPro);
        }
        else if ([name hasPrefix:@"  "] && ![name hasPrefix:@"    "]) {
            //建立市字典
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setObject:name forKey:@"cityName"];
            //建立区数组
            NSMutableArray *areaArr = [NSMutableArray array];
            [dicOfCity setObject:areaArr forKey:@"areaArr"];
            NSMutableDictionary *cityDic = [proArr lastObject];
            NSMutableArray *marrCity = cityDic[@"cityArr"];
            [marrCity addObject:dicOfCity];
            //NSLog(@"111%@",dicOfCity);
        }else {
            NSMutableDictionary *proDic = [proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *areaArr = cityDic[@"areaArr"];
            [areaArr addObject:name];
            //NSLog(@"%@",areaArr);
        }
    }
    //遍历省数组 获取字典 根据key值 获取省名
    for ( NSMutableDictionary *pro in proArr) {
        NSLog(@"%@",pro[@"proName"]);
        //获取市数组
        NSMutableArray *marrOfCity = pro[@"cityArr"];
        for (NSMutableDictionary *city in marrOfCity) {
            NSLog(@"%@",city[@"cityName"]);
            //获取区数组
            NSMutableArray *areaArr = city[@"areaArr"];
            for (NSString *name in areaArr) {
                NSLog(@"%@",name);
            }
            
        }
    }
    NSLog(@"%@",proArr);
    //2.省数组
    /** 2. 将数组字典的处理方式转换成数组模型(Class) */
    // 省数组 - Province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in arrOfPlace) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.marr addObject:city];
        } else {
            
            City *city = [[[allProArray lastObject] marr] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.marr addObject:zone];
        }
    }
    NSLog(@"%@",allProArray);
    
//
    
    
#endif
//     五、拓展
//     上述作业做完, 处理Location1.txt
//     要求同第四题。自己寻找规律
#if 0
    NSString *dataStr = [NSString stringWithContentsOfFile:@"/Users/dllo/Downloads/Homework-05/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfAll = [dataStr componentsSeparatedByString:@","];
    // 创建省数组
    NSMutableArray *proArr = [NSMutableArray array];
       for (NSString *str in arrOfAll) {
       if ([str hasSuffix:@"0000"]) {
         NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
                [proDic setObject:str forKey:@"proName"];
                NSMutableArray *cityArr = [NSMutableArray array];
                [proDic setObject:cityArr forKey:@"cityArr"];
            [proArr addObject:proDic];
        }else if ([str hasSuffix:@"00"]){
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:str forKey:@"cityName"];
            NSMutableArray *arearArr = [NSMutableArray array];
            [cityDic setObject:arearArr forKey:@"areaArr"];
            //市前两位相同的为一个省
            for (NSMutableDictionary *mDic in proArr) {
                if ([[[[[mDic objectForKey:@"proName"] componentsSeparatedByString:@"|"]lastObject] substringWithRange:NSMakeRange(0, 2)] isEqualToString:[[[str componentsSeparatedByString:@"|"]lastObject]substringWithRange:NSMakeRange(0, 2)]]) {
                    NSMutableArray *marrCity = mDic[@"cityArr"];
                    [marrCity addObject:cityDic];
                }
            }
            
        }else {
            for (NSMutableDictionary *mDic in proArr) {
                if ([[[[[mDic objectForKey:@"proName"] componentsSeparatedByString:@"|"]lastObject] substringWithRange:NSMakeRange(0, 2)] isEqualToString:[[[str componentsSeparatedByString:@"|"]lastObject]substringWithRange:NSMakeRange(0, 2)]]) {
                    NSMutableArray *marrCity = mDic[@"cityArr"];
                    //区前三位相同 为一个市
                    for (NSMutableDictionary *cityMdic in marrCity) {
                        if ([[[[[cityMdic objectForKey:@"cityName"]componentsSeparatedByString:@"|"]lastObject]substringWithRange:NSMakeRange(0, 3)]isEqualToString:[[[str componentsSeparatedByString:@"|"]lastObject]substringWithRange:NSMakeRange(0, 3)]]) {
                            NSMutableArray *marr = cityMdic[@"areaArr"];
                            [marr addObject:str];
                            
                        }
                    }
                }
            }
        }
    }
    NSLog(@"%@",proArr);
#endif
#pragma mark ------------  老师  -----------
#if 0
    NSString *source = [NSString stringWithContentsOfFile:@"/Users/dllo/Downloads/Homework-05/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    // 根据, 分割字符串, 得到所有城市的数据
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的|处理字符串, 重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据|切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        // 根据切割生成的数组, 生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        // 添加到新数组中
        [allCityDicArray addObject:cityDic];
    }
    // 排序(重要)
    NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort1]];
    //    NSLog(@"%@", allCityDicArray);
    
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
            
        } else if ([cityNumber hasSuffix:@"00"]) {
            
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
            
            NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            //            NSMutableDictionary *proDic = [finalArray lastObject];
            //            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            //            NSMutableDictionary *dicCity = [cityArray lastObject];
            //            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
   // NSLog(@"1111%@", finalArray);
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *temcity =  [[TempCity alloc]initWithName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:temcity];
    }
    
    // 对 类数组 排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc]initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    //NSLog(@"%@",allTempCityArray);
    NSMutableArray *finalClassArr = [NSMutableArray array];
    for (TempCity *tempcity in allTempCityArray) {
        if ([tempcity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWtihTempCity:tempcity];
            [finalClassArr addObject:pro];
        } else if ([tempcity.cityNumber hasSuffix:@"00"] && [tempcity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArr lastObject];
            City *city = [City zoneWtihTempCity:tempcity];
            [pro.marr addObject:city];
        }else {
            Province *pro = [finalClassArr lastObject];
            City *city = [pro.marr lastObject];
            Zone *zone = [Zone zoneWtihTempCity:tempcity];
            [city.marr addObject:zone];
        }
    }
    
    NSLog(@"%@",finalClassArr);
    
    
#endif
    
    
    
    //     六、如果都做完
//     
//     额外找我要作业, 练习有的是, 就怕你不练
//    
    
    return 0;
}
